//index.js
const util = require("../../utils/util.js");
Page({
  data: {
    marketList: [],
    topTabItems: [],
    currentTopItem: "0",
    swiperHeight: "0",
    users: wx.getStorageSync('USERS')
  },
  //生命周期函数-监听页面初次渲染完毕
  onReady: function () {
    var that = this;
    util.showLoading();
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          swiperHeight: (res.windowHeight - 37)
        });
      }
    });
    util.hideToast();
  },
  //生命周期函数，监听页面加载,页面初始化 options为页面跳转所带来的参数
  onLoad: function (options) {
    this.onLogin();
    this.onPlatCoins();
    this.onMarketData();
  },
  onLogin: function () {
    var that = this;
    var hasLogin = false;
    wx.checkSession({
      success: function (options) {
        if (options.errMsg == "checkSession:ok") {
          that.setData({
            users: wx.getStorageSync('USERS')
          });
          if (!that.data.users.id) {
            hasLogin = true;
          }
        }
      }
    });
    wx.login({
      success: function (options) {
        if (options.code) {
          util.wxPost({
            url: "/rest/login", params: { "code": options.code },
            success: function (data) {
              that.setData({
                users: data.data.data
              });
              wx.setStorage({
                key: "USERS",
                data: data.data.data,
              });
            }
          });
        }
      },
      fail: function () {
        console.log("登录想睡觉了");
      }
    });
  },
  //加载顶部平台
  onPlatCoins: function () {
    var that = this;
    var url = "/rest/coins";
    util.wxGet({
      url: url, data: "",
      success: function (data) {
        that.setData({
          topTabItems: data.data.data
        });
      }
    });
  },
  //加载行情数据
  onMarketData: function () {
    var that = this;
    var platCoin = "ZB";
    for (var index in that.data.topTabItems) {
      if (index == that.data.currentTopItem) {
        platCoin = that.data.topTabItems[index].platCoin;
        that.setData({ marketList: [] });
        break;
      }
    }
    that.setData({
      marketList: wx.getStorageSync(platCoin)
    });
    if (!that.data.marketList.length > 0) {
      var interval = setInterval(function () {
        if (that.data.users.id) {
          var options = {
            "platCoin": platCoin,
            "uid": that.data.users.id
          }
          that.onReadTicker(options);
          clearInterval(interval);
        }
      }, 1 * 500);
    }
  },
  onReadTicker: function (options) {
    var that = this;
    util.wxPost({
      url: "/rest/ticker", params: { "platCoin": options.platCoin, "uid": options.uid },
      success: function (data) {
        that.setData({
          marketList: data.data.data
        });
        wx.setStorage({
          key: options.platCoin,
          data: data.data.data
        });
      }
    });
  },
  //切换顶部标签
  switchTab: function (e) {
    var that = this;
    if (that.data.currentTopItem == e.target.dataset.idx) {
      return false;
    } else {
      that.setData({
        currentTopItem: e.target.dataset.idx
      });
    }
  },
  //swiperChange
  bindChange: function (e) {
    var that = this;
    that.setData({
      currentTopItem: e.detail.current
    });
  },
  //点击单元显示下拉
  kindToggle: function (e) {
    var id = e.currentTarget.id, dataList = this.data.marketList;
    for (var i = 0, len = dataList.length; i < len; ++i) {
      if (dataList[i].id == id) {
        dataList[i].open = !dataList[i].open
      } else {
        dataList[i].open = false
      }
    }
    this.setData({
      marketList: dataList
    });
  },
  onPullDownRefresh: function () {
    var that = this;
    var platCoin = "ZB";
    for (var index in that.data.topTabItems) {
      if (index == that.data.currentTopItem) {
        platCoin = that.data.topTabItems[index].platCoin;
        that.setData({ marketList: [] });
        break;
      }
    }
    util.wxPost({
      url: "/rest/ticker", params: { "platCoin": platCoin, "uid": that.data.users.id },
      success: function (data) {
        that.setData({
          marketList: data.data.data
        });
        wx.setStorage({
          key: platCoin,
          data: data.data.data
        });
        wx.stopPullDownRefresh();
      }
    });
  },
  onBindPlat:function(){
    wx.showToast({
      icon: "none",
      title: "请先前往个人中心配置平台密钥!",
      duration: 2000
    });
  }
})
